﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using hou_elearning.Business;
using hou_elearning.Entity;

public partial class Admin_control_ucHethong : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            napDdlThutruong();
            napThongtindonvi();
            enableEdit(false);
        }
    }

    /// <summary>
    /// Bật, tắt edit
    /// </summary>
    /// <author>liemqv</author>
    /// <create>09/06/2012</create>
    /// <param name="bEdit">Boolean: cho biết trạng thái bật hay tắt</param>
    /// 
    public void enableEdit(Boolean bEdit)
    {
        txtTendonvi.Enabled = bEdit;
        txtDiachi.Enabled = bEdit;
        txtSodienthoai.Enabled = bEdit;
        txtChucvu.Enabled = bEdit;
        txtUrlAnhminhhoa.Enabled = bEdit;
        ddlThutruong.Enabled = bEdit;
        //
        lbtnEdit.Visible = !bEdit;
        btnOK.Visible = bEdit;
    }

    /// <summary>
    /// Nạp dropDownList thủ trưởng đơn vị
    /// </summary>
    /// <author>liemqv</author>
    /// <create>09/06/2012</create>
    ///
    public void napDdlThutruong()
    {
        List<NguoidungEntity> lstNguoidung = new List<NguoidungEntity>();
        List<Nguoidung_nhomEntity> lstNguoidungNhom = Nguoidung_nhomBRL.GetByGetBysKyHieuNhomnguoidung("LD");
        foreach(Nguoidung_nhomEntity oNguoidungNhom in lstNguoidungNhom)
        {
            lstNguoidung.Add(NguoidungBRL.GetOne(oNguoidungNhom.FK_iNguoidungID));
        }
        ddlThutruong.DataSource = lstNguoidung;
        ddlThutruong.DataTextField = "sHoten";
        ddlThutruong.DataValueField = "PK_iNguoidungID";
        ddlThutruong.DataBind();
        lstNguoidung = null;
        lstNguoidungNhom = null;
    }

    /// <summary>
    /// Nạp thông tin đơn vị
    /// </summary>
    /// <author>liemqv</author>
    /// <create>09/06/2012</create>
    ///
    public void napThongtindonvi()
    {
        DonviquanlyEntity oDonvi = DonviquanlyBRL.GetOne(1);
        txtTendonvi.Text = oDonvi.sTendonviquanly;
        txtDiachi.Text = oDonvi.sDiachi;
        txtSodienthoai.Text = oDonvi.sSodienthoai;
        txtChucvu.Text = oDonvi.sChucvuthutruong;
        txtUrlAnhminhhoa.Text = oDonvi.sLogo;
        imgAnhmihhoa.ImageUrl = oDonvi.sLogo;
        if (ddlThutruong.Items.Count > 0)
        {
            ddlThutruong.SelectedItem.Selected = false;
            ddlThutruong.Items.FindByValue(oDonvi.FK_iThutruongID.ToString()).Selected = true;
        }
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        DonviquanlyEntity oDonvi = DonviquanlyBRL.GetOne(1);
        oDonvi.sTendonviquanly = txtTendonvi.Text;
        oDonvi.sDiachi = txtDiachi.Text;
        oDonvi.sSodienthoai = txtSodienthoai.Text;
        oDonvi.sChucvuthutruong = txtChucvu.Text;
        oDonvi.sLogo = txtUrlAnhminhhoa.Text;
        oDonvi.FK_iThutruongID = Int64.Parse(ddlThutruong.SelectedValue);
        if(DonviquanlyBRL.Edit(oDonvi) == true)
        {
            divThongbao.InnerHtml = "<p class='info' id='success'><span class='info_inner'>Cập nhật thành công!</span></p>";
        }
        else
        {
            divThongbao.InnerHtml = "<p class='info' id='error'><span class='info_inner'>Cập nhật thất bại!</span></p>";
        }
        napThongtindonvi();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        enableEdit(false);
    }
    protected void lbtnEdit_Click(object sender, EventArgs e)
    {
        enableEdit(true);
    }
}